package com.yydream.controller;

import com.yydream.common.utils.R;
import com.yydream.common.valid.AddGroup;
import com.yydream.dto.CommonUserDTO;
import com.yydream.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "登录模块功能接口")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation("登录接口")
    @PostMapping("/common/login")
    public R login(@Validated(AddGroup.class) @RequestBody CommonUserDTO userDto, HttpServletRequest request){
        //登录
        return loginService.login(userDto, request);
    }

    @ApiOperation("退出登录")
    @PostMapping("/common/logout")
    public R logout(){
        return loginService.logout();
    }

    @ApiOperation("注册接口")
    @PostMapping("/common/register")
    public R register(@Validated(AddGroup.class) @RequestBody CommonUserDTO userDto, HttpServletRequest request) {
        return loginService.register(userDto, request);
    }

    @ApiOperation("获取验证码")
    @GetMapping("/common/captcha")
    public R forgot(HttpServletRequest request) {
        return loginService.captcha(request);
    }

    @ApiOperation("找回密码")
    @PostMapping("/common/forgot")
    public R forgot(@Validated(AddGroup.class) @RequestBody CommonUserDTO userDto) {
//        TODO: 暂未实现
        return R.ok().put("正在努力研发，啦啦啦!");
//        return loginService.forgot(userDto);
    }

    @ApiOperation("上传文件")
    @PostMapping("/common/uploadFile")
    public R uploadFile(@RequestParam(name = "file")MultipartFile file) {
        return loginService.uploadFile(file);
    }

    @ApiOperation("上传图片")
    @PostMapping("/common/uploadImg")
    public R uploadImg(@RequestParam(name = "file") MultipartFile file) {
        return loginService.uploadImg(file);
    }


}
